{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "e4b210b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "ad5557e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 1. url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'NhBooJ_1o9fLID_ulAEJFigqvwmnVbO3'\n",
    "API_sercret = 'sBy3LVppVXQOpJoPqWmFQoHUxeqTimA0'\n",
    "\n",
    "# 3. POST\n",
    "\n",
    "\n",
    "# 4. 所有 API Key 都可以调用本 API。\n",
    "#    其中 calculate_all 和 face_rectangle 参数只有正式 API Key 才能使用，试用 API Key 无法使用。\n",
    "\n",
    "# 5. payload： 酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'image_url':'https://img0.baidu.com/it/u=1423910105,2964733703&fm=253&fmt=auto&app=138&f=JPG?w=711&h=500'    \n",
    "}\n",
    "\n",
    "r = requests.post(base_url, params = payload)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "717e6b12",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "cf24ca3c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1657283169,624b9a83-51df-46a9-b4f1-82957600f4a9',\n",
       " 'time_used': 617,\n",
       " 'faces': [{'face_token': '888e5a569c2b062140e8de3e2b5ac71c',\n",
       "   'face_rectangle': {'top': 144, 'left': 330, 'width': 211, 'height': 211}}],\n",
       " 'image_id': 'D80Fyvn/7VeJo7ZiqCp21g==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "c82abb66",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'888e5a569c2b062140e8de3e2b5ac71c'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_detect = r.json()\n",
    "face_tokens = face_detect['faces'][0]['face_token']\n",
    "face_tokens"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "98735d86",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "3482d5d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "Analyze_base_url = 'https://api-cn.faceplusplus.com/facepp/v3/face/analyze'\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'NhBooJ_1o9fLID_ulAEJFigqvwmnVbO3'\n",
    "API_sercret = 'sBy3LVppVXQOpJoPqWmFQoHUxeqTimA0'\n",
    "Face_tokens = '888e5a569c2b062140e8de3e2b5ac71c'\n",
    "# 3. POST\n",
    "\n",
    "\n",
    "# 4. 所有 API Key 都可以调用本 API。\n",
    "#    其中 calculate_all 和 face_rectangle 参数只有正式 API Key 才能使用，试用 API Key 无法使用。\n",
    "\n",
    "# 5. payload： 酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_tokens':Face_tokens,\n",
    "    'return_attributes':'gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus',\n",
    "    'beauty_score_min':0,\n",
    "    'beauty_score_max':100\n",
    "    \n",
    "}\n",
    "\n",
    "# 6. requests\n",
    "\n",
    "r_analyze = requests.post(url = Analyze_base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "fe71bc6f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_analyze"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "de45b90a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 429,\n",
       " 'request_id': '1657283564,b257277c-bd80-4c8d-b9ce-f38d2cb8f1fe',\n",
       " 'faces': [{'attributes': {'emotion': {'sadness': 0.007,\n",
       "     'neutral': 49.997,\n",
       "     'disgust': 0.007,\n",
       "     'anger': 49.965,\n",
       "     'surprise': 0.011,\n",
       "     'fear': 0.007,\n",
       "     'happiness': 0.007},\n",
       "    'beauty': {'female_score': 96.145, 'male_score': 95.126},\n",
       "    'gender': {'value': 'Female'},\n",
       "    'age': {'value': 19},\n",
       "    'mouthstatus': {'close': 98.791,\n",
       "     'surgical_mask_or_respirator': 0.0,\n",
       "     'open': 1.209,\n",
       "     'other_occlusion': 0.0},\n",
       "    'glass': {'value': 'None'},\n",
       "    'skinstatus': {'dark_circle': 8.046,\n",
       "     'stain': 6.26,\n",
       "     'acne': 0.554,\n",
       "     'health': 80.168},\n",
       "    'headpose': {'yaw_angle': -0.81452817,\n",
       "     'pitch_angle': 8.531936,\n",
       "     'roll_angle': -0.56333935},\n",
       "    'blur': {'blurness': {'threshold': 50.0, 'value': 0.49},\n",
       "     'motionblur': {'threshold': 50.0, 'value': 0.49},\n",
       "     'gaussianblur': {'threshold': 50.0, 'value': 0.49}},\n",
       "    'smile': {'threshold': 50.0, 'value': 0.0},\n",
       "    'eyestatus': {'left_eye_status': {'normal_glass_eye_open': 0.062,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.105,\n",
       "      'no_glass_eye_open': 99.64,\n",
       "      'normal_glass_eye_close': 0.003,\n",
       "      'dark_glasses': 0.19},\n",
       "     'right_eye_status': {'normal_glass_eye_open': 0.009,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.0,\n",
       "      'no_glass_eye_open': 99.99,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.001}},\n",
       "    'facequality': {'threshold': 70.1, 'value': 92.242},\n",
       "    'eyegaze': {'right_eye_gaze': {'position_x_coordinate': 0.487,\n",
       "      'vector_z_component': 0.96,\n",
       "      'vector_x_component': -0.015,\n",
       "      'vector_y_component': -0.278,\n",
       "      'position_y_coordinate': 0.445},\n",
       "     'left_eye_gaze': {'position_x_coordinate': 0.504,\n",
       "      'vector_z_component': 0.955,\n",
       "      'vector_x_component': -0.035,\n",
       "      'vector_y_component': -0.296,\n",
       "      'position_y_coordinate': 0.4}}},\n",
       "   'face_rectangle': {'width': 211, 'top': 144, 'left': 330, 'height': 211},\n",
       "   'face_token': '888e5a569c2b062140e8de3e2b5ac71c'}]}"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_analyze.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9c96464b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "d8b2061c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'NhBooJ_1o9fLID_ulAEJFigqvwmnVbO3'\n",
    "API_sercret = 'sBy3LVppVXQOpJoPqWmFQoHUxeqTimA0'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "# 4. 准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'image_url':'https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7'\n",
    "}\n",
    "\n",
    "# 5. 发起请求\n",
    "r = requests.post(url = base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "ab6658fe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'7de501f1f566db575e142baa149dd8a6'"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token01 = r.json()[\"faces\"][0]['face_token']\n",
    "face_token01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "93f6a0e9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'NhBooJ_1o9fLID_ulAEJFigqvwmnVbO3'\n",
    "API_sercret = 'sBy3LVppVXQOpJoPqWmFQoHUxeqTimA0'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "# 4. 准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'image_url':'https://tse2-mm.cn.bing.net/th/id/OIP-C.Xz3HbYZeNrdUnGJ7vXNzsQHaKO?pid=ImgDet&rs=1'\n",
    "}\n",
    "\n",
    "# 5. 发起请求\n",
    "r = requests.post(url = base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "15ae0061",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'62996173576ac4eac7638d3b7c1a6c3e'"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token02 = r.json()[\"faces\"][0]['face_token']\n",
    "face_token02"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "cba553ea",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "compare_url = 'https://api-cn.faceplusplus.com/facepp/v3/compare'\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'NhBooJ_1o9fLID_ulAEJFigqvwmnVbO3'\n",
    "API_sercret = 'sBy3LVppVXQOpJoPqWmFQoHUxeqTimA0'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "# 4. 准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_token1':face_token01,\n",
    "    'face_token2':face_token02\n",
    "}\n",
    "\n",
    "# 5. 发起请求\n",
    "r = requests.post(url = compare_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "7a1865b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "a3e04dc8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1657283960,b02e8b75-afbc-45fb-b22b-fc74c2f24873',\n",
       " 'time_used': 265,\n",
       " 'confidence': 83.194,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "996afd39",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
